Imports System
Imports System.Data
Imports System.Windows.Forms

Namespace XPControl
    Public NotInheritable Class DataComponent
#Region "InputLanguage Function                     "
        Public Overloads Shared Function InputLanguage(ByVal value As String) As String
            Dim str As String = ""
            Select Case value
                Case "US"
                    str = "en"
                Case "Thai Kedmanee"
                    str = "th"
            End Select

            Return str

        End Function

        Public Overloads Shared Function OutputLanguage(ByVal value As String) As String
            Dim str As String = ""
            Select Case value
                Case "US"
                    str = "US"
                Case "en"
                    str = "US"
                Case "Thai Kedmanee"
                    str = "TH"
            End Select
            Return str
        End Function
#End Region

#Region "RefreshDataIndex                           "
        Public Overloads Shared Sub RefreshDataIndex(ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            While record <= Arg.Items.Count - 1
                'Arg.Items(record).SubItems(0).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.PaleTurquoise, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub

        Public Overloads Shared Sub RefreshDataIndex(ByVal Column As String, ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            Dim icolumn As Integer = 0
            While icolumn <= Arg.Columns.Count - 1
                If Column = Arg.Columns.Item(icolumn).Text Then
                    Exit While
                End If
                icolumn = icolumn + 1
            End While

            While record <= Arg.Items.Count - 1
                'Arg.Items(record).SubItems(icolumn).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.PaleTurquoise, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub

        Public Overloads Shared Sub RefreshDataIndex(ByVal Column As Integer, ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            While record <= Arg.Items.Count - 1
                ' Arg.Items(record).SubItems(Column).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.PaleTurquoise, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub

        Public Overloads Shared Sub RefreshDataIndex(ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            While record <= Arg.Items.Count - 1
                ' Arg.Items(record).SubItems(0).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.PaleTurquoise, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub

        Public Overloads Shared Sub RefreshDataIndex(ByVal Column As String, ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            Dim icolumn As Integer = 0
            While icolumn <= Arg.Columns.Count - 1
                If Column = Arg.Columns.Item(icolumn).Text Then
                    Exit While
                End If
                icolumn = icolumn + 1
            End While

            While record <= Arg.Items.Count - 1
                ' Arg.Items(record).SubItems.Item(icolumn).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.LemonChiffon, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub

        Public Overloads Shared Sub RefreshDataIndex(ByVal Column As Integer, ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListView)
            Dim record As Integer = 0
            While record <= Arg.Items.Count - 1
                'Arg.Items(record).SubItems.Item(Column).Text = (record + 1).ToString
                SetBGColor(System.Drawing.Color.PaleTurquoise, System.Drawing.Color.White, Arg.Items(record))
                record = record + 1
            End While
        End Sub
#End Region

#Region "AddColumnNo                                "
        Public Overloads Shared Function AddColumnNo(ByVal Arg As System.Data.DataTable) As System.Data.DataTable
            If Not Arg Is Nothing Then
                If Arg.Rows.Count > 0 Then
                    Dim record As Integer = 0
                    'Dim drow As System.Data.DataRow

                    If Not Arg.Columns.Item("No") Is Nothing Then
                        Arg.Columns.Remove("No")
                    End If
                    Arg.Columns.Add("No")

                    While record <= Arg.Rows.Count - 1
                        Arg.Rows(record).Item("No") = record + 1
                        record = record + 1
                    End While
                End If
            End If

            Return Arg
        End Function

        Public Overloads Shared Function AddColumnNo(ByVal Arg As System.Data.DataTable, ByVal sColumn As String) As System.Data.DataTable
            If Not Arg Is Nothing Then
                If Arg.Rows.Count > 0 Then
                    Dim record As Integer = 0
                    'Dim drow As System.Data.DataRow

                    If Not Arg.Columns.Item(sColumn) Is Nothing Then
                        Arg.Columns.Remove(sColumn)
                    End If
                    Arg.Columns.Add(sColumn)

                    While record <= Arg.Rows.Count - 1
                        Arg.Rows(record).Item(sColumn) = record + 1
                        record = record + 1
                    End While
                End If
            End If

            Return Arg
        End Function

        Public Overloads Shared Function AddColumnNo(ByVal Arg As System.Data.DataTable, ByVal iColumn As Integer) As System.Data.DataTable
            If Not Arg Is Nothing Then
                If Arg.Rows.Count > 0 Then
                    Dim record As Integer = 0
                    'Dim drow As System.Data.DataRow
                    Dim ColumnName As String = "Column" & iColumn.ToString

                    If Not Arg.Columns.Item(ColumnName) Is Nothing Then
                        Arg.Columns.Remove(ColumnName)
                    End If
                    Arg.Columns.Add(ColumnName)

                    While record <= Arg.Rows.Count - 1
                        Arg.Rows(record).Item(ColumnName) = record + 1
                        record = record + 1
                    End While
                End If
            End If

            Return Arg
        End Function
#End Region

#Region "SetBGColor                                 "
        Public Overloads Shared Sub SetBGColor(ByVal back As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListViewItem)
            Arg.BackColor = back
        End Sub

        Public Overloads Shared Sub SetBGColor(ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListViewItem)
            If (Arg.Index Mod 2) Then
                Arg.BackColor = back
            Else
                Arg.BackColor = alter
            End If
        End Sub

        Public Overloads Shared Sub SetBGColor(ByVal index As Integer, ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListViewItem)
            If (index Mod 2) Then
                Arg.BackColor = back
            Else
                Arg.BackColor = alter
            End If
        End Sub

        Public Overloads Shared Sub SetBGColor(ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListView)
            If (Arg.Items(Arg.Items.Count - 1).Index Mod 2) Then
                Arg.Items(Arg.Items.Count - 1).BackColor = back
            Else
                Arg.Items(Arg.Items.Count - 1).BackColor = alter
            End If
        End Sub

        Public Overloads Shared Sub SetBGColor(ByVal index As Integer, ByVal back As System.Drawing.Color, ByVal alter As System.Drawing.Color, ByVal Arg As System.Windows.Forms.ListView)
            If (Arg.Items(index).Index Mod 2) Then
                Arg.Items(index).BackColor = back
            Else
                Arg.Items(index).BackColor = alter
            End If
        End Sub
#End Region

#Region "Set BackColor TextBox                      "
        Public Overloads Shared Sub TextBox_GotFocus(ByVal TextBox As System.Windows.Forms.TextBox)
            TextBox.ForeColor = System.Drawing.Color.Black
            TextBox.BackColor = System.Drawing.Color.SkyBlue
        End Sub

        Public Overloads Shared Sub TextBox_LostFocus(ByVal TextBox As System.Windows.Forms.TextBox)
            TextBox.ForeColor = System.Drawing.SystemColors.ControlText
            TextBox.BackColor = System.Drawing.SystemColors.Window
        End Sub

        Public Overloads Shared Sub UpDown(ByVal TextBox1 As System.Windows.Forms.TextBox, ByVal TextBox2 As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)
            Select Case e.KeyCode
                Case Keys.Up
                    TextBox1.Focus()
                Case Keys.Down
                    TextBox2.Focus()
            End Select
        End Sub
#End Region

#Region "Compare string in ListView                 "
        Public Overloads Shared Function CompareString(ByVal Arg As System.Windows.Forms.ListView) As Boolean
            Dim irecord As Integer = 0
            While irecord <= Arg.Items.Count - 1
                If UCase(Arg.Items(irecord).SubItems(0).Text) = UCase(Arg.Items(irecord).SubItems(0).Text) Then
                    Return True
                End If
                irecord = irecord + 1
            End While
            Return False
        End Function

        Public Overloads Shared Function CompareString(ByVal strText As String, ByVal Arg As System.Windows.Forms.ListView) As Boolean
            Dim irecord As Integer = 0
            Dim ilentItem As Integer = 0
            Dim ilentText As Integer = strText.Length

            While irecord <= Arg.Items.Count - 1
                ilentItem = Arg.Items(irecord).SubItems(0).Text.Length
                If UCase(Mid(strText, 1, ilentText)) = UCase(Mid(Arg.Items(irecord).SubItems(0).Text, 1, ilentItem)) Then
                    Return True
                End If
                irecord = irecord + 1
            End While
            Return False
        End Function

        Public Overloads Shared Function CompareString(ByVal iColumn As Integer, ByVal strText As String, ByVal Arg As System.Windows.Forms.ListView) As Boolean
            Dim irecord As Integer = 0
            Dim ilentItem As Integer = 0
            Dim ilentText As Integer = strText.Length

            While irecord <= Arg.Items.Count - 1
                ilentItem = Arg.Items(irecord).SubItems(iColumn).Text.Length
                If UCase(Mid(strText, 1, ilentText)) = UCase(Mid(Arg.Items(irecord).SubItems(iColumn).Text, 1, ilentItem)) Then
                    Return True
                End If
                irecord = irecord + 1
            End While
            Return False
        End Function
#End Region

#Region "Find string in ListView                    "
        Public Overloads Shared Function FindString(ByVal strText As String, ByVal Arg As System.Windows.Forms.ListView) As String
            Dim irecord As Integer = 0
            Dim ilentItem As Integer = 0
            Dim ilentText As Integer = strText.Length

            While irecord <= Arg.Items.Count - 1
                ilentItem = Arg.Items(irecord).SubItems(0).Text.Length
                If UCase(Mid(strText, 1, ilentText)) = UCase(Mid(Arg.Items(irecord).SubItems(0).Text, 1, ilentItem)) Then
                    Return Arg.Items(irecord).SubItems(0).Text
                End If
                irecord = irecord + 1
            End While
            Return ""
        End Function

        Public Overloads Shared Function FindString(ByVal iColumn As Integer, ByVal strText As String, ByVal Arg As System.Windows.Forms.ListView) As String
            Dim irecord As Integer = 0
            Dim ilentItem As Integer = 0
            Dim ilentText As Integer = strText.Length

            While irecord <= Arg.Items.Count - 1
                ilentItem = Arg.Items(irecord).SubItems(0).Text.Length
                If UCase(Mid(strText, 1, ilentText)) = UCase(Mid(Arg.Items(irecord).SubItems(iColumn).Text, 1, ilentItem)) Then
                    Return Arg.Items(irecord).SubItems(iColumn).Text
                End If
                irecord = irecord + 1
            End While
            Return ""
        End Function
#End Region

#Region "Application Start Up Path                  "
        Public Overloads Shared Function StartUpPath() As String
            Return Application.StartupPath
        End Function

        Public Overloads Shared Function StartUpPath(ByVal path As String) As String
            If path = "" Then
                Return Application.StartupPath
            Else
                Dim tmp As String = Application.StartupPath
                Return tmp.Substring(0, Application.StartupPath.Length - 4)
            End If
        End Function

        Public Overloads Shared Function StartUpPath(ByVal path As String, ByVal folder As String) As String
            If path = "" Then
                Return Application.StartupPath
            Else
                Return path.Substring(0, path.Length - 4) & "\" & folder
            End If
        End Function

        Public Overloads Shared Function StartHelpPath() As String
            Dim path As String = Application.StartupPath
            Return Application.StartupPath.Substring(0, path.Length - 4) & "\Help"
        End Function

        Public Overloads Shared Function StartHelpPath(ByVal folder As String) As String
            Dim path As String = Application.StartupPath
            Return Application.StartupPath.Substring(0, path.Length - 4) & "\" & folder
        End Function

        Public Overloads Shared Function StartReportPath() As String
            Dim path As String = Application.StartupPath
            Return Application.StartupPath.Substring(0, path.Length - 4) & "\Report"
        End Function

        Public Overloads Shared Function StartReportPath(ByVal folder As String) As String
            Dim path As String = Application.StartupPath
            Return Application.StartupPath.Substring(0, path.Length - 4) & "\" & folder
        End Function
#End Region
    End Class
End Namespace